﻿using Core.ReadAndWrite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Core
{
    public class QuanLiDKHP:IQuanLi
    {

        #region Singleton
        private static QuanLiDKHP _Instance;
        private static object locks = new object();

        public static QuanLiDKHP Instance
        {
            get
            {
                if (_Instance == null)
                {
                    lock (locks)
                    {
                        if (_Instance == null)
                            _Instance = new QuanLiDKHP();
                    }
                }
                return _Instance;
            }
        }
        #endregion
        public List<BaseData> ListHocPhanDK;

        public QuanLiDKHP()
        {
            ListHocPhanDK = DataSource.Instance.ReadDKHP("QLDKHP.txt");
        }
        public void Add(BaseData ob)
        {
            DangKiHocPhan dkhp = ob as DangKiHocPhan;
            if (dkhp != null)
            {
                DangKiHocPhan temp = (DangKiHocPhan)QuanLiDKHP.Instance.Find(x => (x as DangKiHocPhan).MaSV == dkhp.MaSV && (x as DangKiHocPhan).HocKi == dkhp.HocKi
                            && (x as DangKiHocPhan).NamHoc == dkhp.NamHoc);
                if (temp == null)
                {
                    ListHocPhanDK.Add(ob);
                }
            }

        }

        public void Delete(BaseData ob)
        {
            ListHocPhanDK.Remove(ob);
        }

      

        public List<BaseData> FindAll(Predicate<BaseData> Predicate)
        {
            return ListHocPhanDK.FindAll(Predicate);
        }

        public BaseData Find(Func<BaseData, bool> predicate)
        {
            return ListHocPhanDK.FirstOrDefault(predicate);
        }






        public void Update(BaseData ob)
        {
            throw new NotImplementedException();
        }

        public DangKiHocPhan this[int index]
        {
            get
            {
                return ListHocPhanDK[index] as DangKiHocPhan;
            }
        }

        public override string ToString()
        {
            StringBuilder str = new StringBuilder();
            foreach (var item in ListHocPhanDK)
            {
                str.Append(item.ToString());
            }
            return str.ToString();
        }
    }
}
